import request from "@/utils/request";
import type {
  RequestParams,
  ResponseData,
  libraryRequestParams,
  FileInfo,
} from "./type";
import { AxiosPromise } from "axios";
// 分页列表
export const libraryList = (data: RequestParams) => {
  return request<ResponseData<libraryRequestParams>>({
    url: "/api/v1/library/page",
    method: "post",
    data,
    headers: {
      //需要加密的请求在头部塞入标识
      isEncrypt: 1,
    },
  });
};
export const addLibrary = (data: libraryRequestParams) => {
  return request<ResponseData<libraryRequestParams>>({
    url: "/api/v1/library/add",
    method: "post",
    data,
    headers: {
      //需要加密的请求在头部塞入标识
      isEncrypt: 1,
    },
  });
};
export const delLibrary = (id: number) => {
  return request<ResponseData<libraryRequestParams>, any>({
    url: "/api/v1/library/delete",
    method: "post",
    data: id,
    headers: {
      //需要加密的请求在头部塞入标识
      isEncrypt: 1,
    },
  });
};
export const FormLibrary = (id: number) => {
  return request<ResponseData<libraryRequestParams>>({
    url: "/api/v1/library/form",
    method: "post",
    data: id,
    headers: {
      //需要加密的请求在头部塞入标识
      isEncrypt: 1,
    },
  });
};

export const LibraryOptions = (aesKeyByRsa: string) => {
  return request<ResponseData<any>>({
    url: "/api/v1/library/options",
    method: "post",
    data: aesKeyByRsa,
    headers: {
      //需要加密的请求在头部塞入标识
      isEncrypt: 1,
    },
  });
};
export const updateLibrary = (data: libraryRequestParams) => {
  return request<ResponseData<libraryRequestParams>>({
    url: "/api/v1/library/update",
    method: "post",
    data,
    headers: {
      //需要加密的请求在头部塞入标识
      isEncrypt: 1,
    },
  });
};
/**
 * 上传文件
 *
 * @param file
 */
// export function uploadFileApi(file: File): AxiosPromise<FileInfo> {
//   const formData = new FormData();
//   formData.append("file", file);
//   return request({
//     url: "/oss/upload",
//     method: "post",
//     data: formData,
//     headers: {
//       "Content-Type": "multipart/form-data",
//     },
//     timeout: 10000, // 设置超时时间为 30 秒
//   });
// }
const MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB
const UPLOAD_TIMEOUT = 10000; // 10秒
export function uploadFileApi(file: File): AxiosPromise<FileInfo> {
  const formData = new FormData();
  formData.append("file", file);
  return request({
    url: "/oss/upload",
    method: "post",
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data",
    },
    timeout: UPLOAD_TIMEOUT,
  });
}
export const HomeList = (data: RequestParams) => {
  return request<ResponseData<libraryRequestParams>>({
    url: "/api/v1/library/statistics",
    method: "post",
    data,
    headers: {
      //需要加密的请求在头部塞入标识
      isEncrypt: 1,
    },
  });
};
